var drawCall = 0;
function drawArrows(ctx, objs) {
    for (var j = 0; j < objs.length; j++) {
        drawArrow(ctx, objs[j]);
    }
}
function drawArrow(ctx, obj) {
    if (obj.isDraw) {
        var tr_x = obj.x - dx;
        var tr_y = obj.y - dy;
        ctx.save();
        ctx.translate(Math.round(tr_x), Math.round(tr_y));
        ctx.rotate(obj.r * Math.PI / 180);
        ctx.drawImage(
                getPicture('heroes/' + obj.dir, 'arrow', 'png')
                , 150 * Math.round(obj.f)
                , 0
                , 150
                , 50
                , -150
                , -25
                , 150
                , 50);
        ctx.restore();
    }
}
function drawTowers(ctx, objs) {
    for (var j = 0; j < objs.length; j++) {
        drawTower(ctx, objs[j]);
    }
}
function drawTower(ctx, obj) {
    if (obj.isDraw) {
        var tr_x = obj.x - dx;
        var tr_y = obj.y - dy;
        ctx.save();
        ctx.translate(Math.round(tr_x * SCALE), Math.round(tr_y * SCALE));
        ctx.rotate(obj.r * Math.PI / 180);
        ctx.drawImage(
                getPicture('heroes/' + obj.dir, '1', 'png')
                , 250 * Math.round(obj.fw)
                , 0
                , 250
                , 250
                , Math.round(-125 * SCALE)
                , Math.round(-125 * SCALE)
                , Math.round(250 * SCALE)
                , Math.round(250 * SCALE));
        ctx.restore();
    }
}
function drawUnits(ctx, objs) {
    for (var j = 0; j < objs.length; j++) {
        drawUnit(ctx, objs[j]);
    }
}
function drawUnit(ctx, obj) {
    var tr_x = obj.x - dx;
    var tr_y = obj.y - dy;
    ctx.save();
    ctx.translate(Math.round(tr_x * SCALE), Math.round(tr_y * SCALE));
    ctx.rotate(obj.r * Math.PI / 180);
    /* drawCall++;
     ctx.drawImage(
     getPicture('resource/', 'shadow', 'png')
     , -55
     , -55);*/
    if (obj.type_animation == "run" && Math.round(obj.fw) >= 50) {
        $("#log").html("ERROR run");
    }
    if (obj.type_animation == "stand" && Math.round(obj.fw) >= 40) {
        $("#log").html("ERROR stand");
    }
    if ((obj.type_animation == "atack1" || obj.type_animation == "atack2" || obj.type_animation == "atack3") && Math.round(obj.fw) >= 60) {
        $("#log").html("ERROR atack");
    }
    //$("#log").html("1");
    ctx.drawImage(
            //getPicture('heroes/' + obj.dir, obj.type_animation, 'png')
            getAnimationCanvas(obj.heroId, obj.type_animation).canvas
            , Math.round(obj.imgW * SCALE * Math.round(obj.fw))
            , 0
            , Math.round(obj.imgW * SCALE)
            , Math.round(obj.imgW * SCALE)
            , Math.round(-Math.round(obj.imgW / 2) * SCALE)
            , Math.round(-Math.round(obj.imgW / 2) * SCALE)
            , Math.round(obj.imgW * SCALE)
            , Math.round(obj.imgW * SCALE));
    ctx.restore();
}
function drawUnitSelected(ctx) {
    for (var i = 0; i < objects.heroes.length; i++) {
        if (selectedObject.id == objects.heroes[i].id) {
            var tr_x = objects.heroes[i].x - dx;
            var tr_y = objects.heroes[i].y - dy;
            ctx.save();
            ctx.scale(SCALE, SCALE);
            ctx.translate(tr_x, tr_y);
            drawCall++;
            ctx.drawImage(
                    getPicture('resource/', 'selected', 'png')
                    , -55
                    , -55);
            ctx.restore();
        }
    }
}
function drawMeHp(ctx) {
    for (var i = 0; i < objects.heroes.length; i++) {
        if (meId == objects.heroes[i].id) {
            var tr_x = Math.round((objects.heroes[i].x - dx) * SCALE);
            var tr_y = Math.round((objects.heroes[i].y - dy) * SCALE);
            ctx.strokeStyle = "rgb(70,175,40)";
            ctx.lineWidth = 3;
            drawCall++;
            ctx.beginPath();
            ctx.moveTo(tr_x - 20, tr_y - 60);
            ctx.lineTo(tr_x - 20 + Math.round(((objects.heroes[i].hp * 40) / objects.heroes[i].maxHp)), tr_y - 60);
            ctx.stroke();
            //Мана черная полоска + голубая
            canvas.strokeStyle = "rgb(20,20,20)";
            canvas.lineWidth = 5;
            drawCall++;
            ctx.beginPath();
            ctx.moveTo(tr_x - 21, tr_y - 56);
            ctx.lineTo(tr_x + 21, tr_y - 56);
            ctx.stroke();
            canvas.strokeStyle = "rgb(90,140,190)";
            canvas.lineWidth = 3;
            drawCall++;
            ctx.beginPath();
            ctx.moveTo(tr_x - 20, tr_y - 56);
            ctx.lineTo(tr_x + 20 /*+ Math.round(((objects.heroes[i].mp * 40) / objects.heroes[i].maxMp))*/, tr_y - 56);
            ctx.stroke();
        }
    }
}
function drawTowersHp(ctx) {
    ctx.strokeStyle = "rgb(160,10,15)";
    ctx.lineWidth = 3;
    ctx.beginPath();
    for (var i = 0; i < objects.towers.length; i++) {
        if (objects.towers[i].teamId != meTeamId) {
            var tr_x = Math.round((objects.towers[i].x - dx) * SCALE);
            var tr_y = Math.round((objects.towers[i].y - dy) * SCALE);
            ctx.moveTo(tr_x - 50, tr_y - 60);
            ctx.lineTo(tr_x - 50 + Math.round(((objects.towers[i].hp * 100) / objects.towers[i].maxHp)), tr_y - 60);
        }
    }
    drawCall++;
    ctx.stroke();
    ctx.strokeStyle = "rgb(75,110,75)";
    ctx.lineWidth = 3;
    ctx.beginPath();
    for (var i = 0; i < objects.towers.length; i++) {
        if (objects.towers[i].teamId == meTeamId) {
            var tr_x = (objects.towers[i].x - dx) * SCALE;
            var tr_y = (objects.towers[i].y - dy) * SCALE;
            ctx.moveTo(tr_x - 50, tr_y - 60);
            ctx.lineTo(tr_x - 50 + Math.round(((objects.towers[i].hp * 100) / objects.towers[i].maxHp)), tr_y - 60);
        }
    }
    drawCall++;
    ctx.stroke();
}
function drawUnitsHpNew(ctx) {
    for (var i = 0; i < objects.heroes.length; i++) {
        var tr_x = objects.heroes[i].x - dx;
        var tr_y = objects.heroes[i].y - dy;
        ctx.save();
        ctx.translate(Math.round(tr_x * SCALE), Math.round(tr_y * SCALE));
        var hpfw = 0;
        if (objects.heroes[i].teamId == meTeamId && objects.heroes[i].id != meId && objects.heroes[i].typeId == 4) {
            hpfw = 1;//союзные крипы
        } else if (objects.heroes[i].teamId != meTeamId && objects.heroes[i].id != meId && objects.heroes[i].typeId == 4) {
            hpfw = 2;// крипы противника
        } else if (objects.heroes[i].teamId == meTeamId && objects.heroes[i].id != meId && (objects.heroes[i].typeId == 1 || objects.heroes[i].typeId == 2 || objects.heroes[i].typeId == 3)) {
            hpfw = 3;// союзные герои
        } else if (objects.heroes[i].teamId != meTeamId && objects.heroes[i].id != meId && (objects.heroes[i].typeId == 1 || objects.heroes[i].typeId == 2 || objects.heroes[i].typeId == 3)) {
            hpfw = 4;// герои противника
        }
        ctx.drawImage(
                hpCanvas.canvas
                , 60 * hpfw
                , 5 * Math.round(((objects.heroes[i].hp * 60) / objects.heroes[i].maxHp - 1))
                , 60
                , 5
                , (hpfw === 0 || hpfw === 3 || hpfw === 4) ? -30 : -20
                , (hpfw === 0 || hpfw === 3 || hpfw === 4) ? -50 : -40
                , (hpfw === 0 || hpfw === 3 || hpfw === 4) ? 60 : 40
                , 5);
        if (hpfw === 0) {
            ctx.drawImage(
                    hpCanvas.canvas
                    , 60 * 5
                    , 5 * 59//Math.round(((objects.heroes[i].mp * 60) / objects.heroes[i].maxMp - 1))
                    , 60
                    , 5
                    , -30
                    , -44
                    , 60
                    , 5);
        }
        //ctx.fillText(objects.heroes[i].id, -20, -50);
        ctx.restore();
    }
    for (var i = 0; i < objects.towers.length; i++) {
        var tr_x = Math.round((objects.towers[i].x - dx) * SCALE);
        var tr_y = Math.round((objects.towers[i].y - dy) * SCALE);
        ctx.save();
        ctx.translate(tr_x, tr_y);
        var hpfw = 1;
        if (objects.towers[i].teamId != meTeamId) {
            hpfw = 2;
        }
        ctx.drawImage(
                hpCanvas.canvas
                , 60 * hpfw
                , 5 * Math.round(((objects.towers[i].hp * 60) / objects.towers[i].maxHp - 1))
                , 60
                , 5
                , -30
                , -50
                , 60
                , 5);
        ctx.restore();

    }
}
function drawUnitsHp(ctx) {
    ctx.beginPath();
    for (var i = 0; i < objects.heroes.length; i++) {
        var tr_x = Math.round((objects.heroes[i].x - dx) * SCALE);
        var tr_y = Math.round((objects.heroes[i].y - dy) * SCALE);
        ctx.moveTo(tr_x - 21, tr_y - 60);
        ctx.lineTo(tr_x + 21, tr_y - 60);
    }
    for (var i = 0; i < objects.towers.length; i++) {
        var tr_x = Math.round((objects.towers[i].x - dx) * SCALE);
        var tr_y = Math.round((objects.towers[i].y - dy) * SCALE);
        ctx.moveTo(tr_x - 51, tr_y - 60);
        ctx.lineTo(tr_x + 51, tr_y - 60);
    }
    ctx.stroke();
    drawTowersHp(ctx);
    //drawAlliesHeroesHp(ctx);
    //drawAnemyHeroesHp(ctx);
    drawMeHp(ctx);
}
function drawEsplosionBloods(ctx, objs) {
    for (var j = 0; j < objs.length; j++) {
        drawEsplosionBlood(ctx, objs[j]);
    }
}
function drawEsplosionBlood(ctx, obj) {
    var tr_x = obj.x - dx;
    var tr_y = obj.y - dy;
    if (obj.type == 3) {
        tr_x = obj.hero.x - dx;
        tr_y = obj.hero.y - dy;
    }
    ctx.save();
    ctx.translate(Math.round(tr_x), Math.round(tr_y));
    ctx.rotate(obj.r * Math.PI / 180);
    if (obj.type == 1) {
        ctx.drawImage(
                getPicture('resource', 'blood', 'png')
                , 128 * Math.round(obj.f)
                , 0
                , 128
                , 128
                , -128
                , -128
                , 256
                , 256);
    } else if (obj.type == 2) {
        ctx.drawImage(
                getPicture('heroes/' + obj.dir, 'explosion', 'png')
                , 128 * Math.round(obj.f)
                , 0
                , 128
                , 128
                , -50
                , -50
                , 100
                , 100);
    } else if (obj.type == 3) {
        ctx.drawImage(
                getPicture('resource', 'lvlup5', 'png')
                , 250 * Math.round(obj.f)
                , 0
                , 250
                , 250
                , -125
                , -125
                , 250
                , 250);
    }
    ctx.restore();
}